<?php

namespace App\Store\Models\Financial\Business;

use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Financial\Statements\Subject;

class Otherpay extends Model
{
    protected $table = 'site_financial_business_otherpay';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];


    public static function getList(Request $request){
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $query=self::with(['subjectinfo'])->where([['cp_id',$cp_id],['is_del',1]])->orderBy('date_str','desc');
        $store_ids = StoreCommon::getCurrentAccountStoreId($request);
        if($store_ids){//权限控制
            if(is_array($store_ids)){//获取当前权限下的门店
                $query->whereIn('store_id',$store_ids);
            }
            if(is_numeric($store_ids) && intval($store_ids)>0){//获取当前门店下的列表
                $query->where('store_id', intval($store_ids));
            }
        }

        $store_id=intval($request->store_id);
        $abstract=$request->abstract ?? '';
        $subject_id=intval($request->subject_id);
        $date_str_start=$request->date_str_start ?? '';
        $date_str_end=$request->date_str_end ?? '';

        $in_money_start=intval($request->in_money_start);$in_money_end=intval($request->in_money_end);
        $out_money_start=intval($request->out_money_start);$out_money_end=intval($request->out_money_end);

        if(!empty($out_money_end)){
            $out_money_end = Otherpay::endTimeFormat($out_money_end);
        }
        if($store_id>0){
            $where[]=['store_id',$store_id];
        }

        if($abstract!=''){
            // $where[]=['abstract','like','%'.$abstract.'%'];
            $where[]=['abstract',$abstract];
        }
        if($subject_id>0){
            $where[]=['subject_id',$subject_id];
        }

        if(!empty($date_str_start) && !empty($date_str_end)){
            $where[]=['date_str','>=',$date_str_start];
            $where[]=['date_str','<=',$date_str_end];
        }

        if($in_money_start>0 && $in_money_end>$in_money_start){
            $where[]=['in_money','>=',$in_money_start];
            $where[]=['in_money','<=',$in_money_end];
        }

        if($out_money_start>0 && $out_money_end>$out_money_start){
            $where[]=['out_money','>=',$out_money_start];
            $where[]=['out_money','<=',$out_money_end];
        }



        $query->where($where);
        $isgetall = intval($request->isgetall) ?? 0;
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            $list=Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            $list=['list' => $list];
        }

        $sumdata=[
            'in_money'=>$query->sum('in_money'),
            'out_money'=>$query->sum('out_money'),
        ];

        return ['sumdata'=>$sumdata,'list'=>$list];
    }

    public function subjectinfo(){
        return $this->hasOne(Subject::class, 'id', 'subject_id')->select(['id','subject_name']);
    }



    /**
     * 时间处理
     * @param $date
     * @return false|int|string
     */
    public static function endTimeFormat($date)
    {
        if (is_numeric($date)) {
            $date = date("Y-m-d", $date);
            $date = sprintf('%s %s', $date, '23:59:59');
            $date = strtotime($date);
        } else {
            $date = sprintf('%s %s', $date, '23:59:59');
        }
        return $date;
    }
}
